﻿using System;
using DSLPedUtilsTest.TestingObjects;
using Dsl.Ped.Editor.Helpers;
using NUnit.Framework;

namespace DSLPedUtilsTest.Utils
{
    /// <summary>
    /// Tests of the <see cref="XmlHelper"/> deserialize method
    /// </summary>
    [TestFixture]
    public class XmlUtilsDeserializeTest
    {
        private const string NoEncodingFile = "TestingFiles/ProjNoEncoding.xml";
        private const string Utf8File = "TestingFiles/ProjUTF8.xml";
        private const string Utf8FileCorrupted = "TestingFiles/ProjUTF8Corrupted.xml";
        private const string Utf16File = "TestingFiles/ProjUTF16.xml";
        private const string Utf8NoNamespaceFile = "TestingFiles/ProjUTF8NoNamespace.xml";

        [SetUp]
        public void SetUpForEachTest()
        {
            // Not used section
        }

        [Test]
        public void DeserializeXmlContentUTF8Test()
        {
            var xmlContent = FileHelper.GetFileContent(Utf8File);
            var xmlDocument = XmlHelper.DeserializeXmlContent(typeof(Proj), "http://Proj", xmlContent);
            Assert.NotNull(xmlDocument);
        }

        [Test]
        public void DeserializeXmlContentUTF16Test()
        {
            var xmlContent = FileHelper.GetFileContent(Utf16File);
            var xmlDocument = XmlHelper.DeserializeXmlContent(typeof(Proj), "http://Proj", xmlContent);
            Assert.NotNull(xmlDocument);
        }


        [Test]
        public void DeserializeXmlContentNoEncodingTest()
        {
            var xmlContent = FileHelper.GetFileContent(NoEncodingFile);
            var xmlDocument = XmlHelper.DeserializeXmlContent(typeof(Proj), "http://Proj", xmlContent);
            Assert.NotNull(xmlDocument);
        }

        [Test]
        public void DeserializeXmlContentWrongObjectTest()
        {
            try
            {
                var xmlContent = FileHelper.GetFileContent(Utf8File);
                XmlHelper.DeserializeXmlContent(typeof (peoples), "http://Proj", xmlContent);
            } catch (Exception exc)
            {
                Assert.AreEqual(true, exc.Message.StartsWith("Unable to deserialize the Xml content"));
            }
        }

        [Test]
        public void DeserializeXmlContentCorruptedXmlTest()
        {
            try
            {
                var xmlContent = FileHelper.GetFileContent(Utf8FileCorrupted);
                XmlHelper.DeserializeXmlContent(typeof(Proj), "http://Proj", xmlContent);
            }
            catch (Exception exc)
            {
                Assert.AreEqual(true, exc.Message.StartsWith("Unable to deserialize the Xml content"));
            }
        }

        [Test]
        public void DeserializeXmlNoNamespaceSpecifiedTest()
        {
            try
            {
                var xmlContent = FileHelper.GetFileContent(Utf8NoNamespaceFile);
                XmlHelper.DeserializeXmlContent(typeof(Proj), "http://Proj", xmlContent);
            }
            catch (Exception exc)
            {
                Assert.AreEqual(true, exc.Message.StartsWith("Unable to deserialize the Xml content"));
            }
        }

        [Test]
        public void DeserializeXmlFileUTF8Test()
        {
            var xmlDocument = XmlHelper.DeserializeXmlFile(typeof(Proj), "http://Proj", Utf8File);
            Assert.NotNull(xmlDocument);
        }

        [Test]
        public void DeserializeXmlFileUTF16Test()
        {
            var xmlDocument = XmlHelper.DeserializeXmlFile(typeof(Proj), "http://Proj", Utf16File);
            Assert.NotNull(xmlDocument);
        }

        [Test]
        public void DeserializeXmlFileNoEncodingTest()
        {
            var xmlDocument = XmlHelper.DeserializeXmlFile(typeof(Proj), "http://Proj", NoEncodingFile);
            Assert.NotNull(xmlDocument);
        }

        [Test]
        public void DeserializeXmlFileCorruptedXmlTest()
        {
            try
            {
                XmlHelper.DeserializeXmlFile(typeof(Proj), "http://Proj", Utf8FileCorrupted);
            }
            catch (Exception exc)
            {
                Assert.AreEqual(true, exc.Message.StartsWith("Unable to deserialize the Xml content"));
            }
        }
    }
}
